package org.example.ui.customer;
import org.example.business.MilkTeaBusiness;
import org.example.business.AddToCartBusiness;
import org.example.business.OrderBusiness;
import org.example.model.Order;
import org.example.ui.PromptComboBox;
import org.example.ui.SearchField;
import org.example.ui.milk_order.MilkTeaItemPanel;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class OrderPanel extends JPanel {
    private static JScrollPane scrollPane, kindScrollPanel;
    private JScrollPane cartScrollPanel;
    private static SpringLayout layout, kindLayout, teaLayout;
    private final SearchField search;
    private JButton buyButton = new JButton("去结算"), emptyButton = new JButton("\uD83D\uDDD1\uFE0F");
    private static JPanel kindPanel = new JPanel(), teaPanel = new JPanel(), searchPanel;
    private static JPanel jPanel;
    private final JLabel cartLabel;
    private static JLabel numberLabel;
    private PromptComboBox comboBox = new PromptComboBox(0);
    private boolean b = true;
    public OrderPanel() {
        jPanel = new JPanel();
        layout = new SpringLayout();
        cartLabel = new JLabel("   "+"\uD83D\uDED2");
        numberLabel = new JLabel();
        kindLayout =  new SpringLayout();
        kindPanel = new JPanel();
        teaPanel = new JPanel();
        jPanel.setBackground(null);
        jPanel.setOpaque(false);
        cartScrollPanel = new JScrollPane(jPanel);
        cartScrollPanel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
        cartScrollPanel.getVerticalScrollBar().setUnitIncrement(20);
        teaLayout = new SpringLayout();
        search = new SearchField();
        scrollPane = new JScrollPane(teaPanel);
        kindScrollPanel = new JScrollPane(kindPanel);
        MilkTeaBusiness.teaPanel = teaPanel;
        MilkTeaBusiness.kindPanel = kindPanel;
        MilkTeaBusiness.scrollPane = scrollPane;
        MilkTeaBusiness.kindScrollPanel = kindScrollPanel;
        MilkTeaBusiness.teaLayout = teaLayout;
        MilkTeaBusiness.kindLayout = kindLayout;
        AddToCartBusiness.jPanel = jPanel;
        AddToCartBusiness.numberLabel = numberLabel;
        MilkTeaBusiness.setMilkTeas(0);
        MilkTeaBusiness.setKinds();
        teaPanel.setBackground(Color.WHITE);
        kindPanel.setBackground(null);
        kindPanel.setOpaque(false);
        kindScrollPanel.setOpaque(false);
        kindScrollPanel.getViewport().setOpaque(false);
        teaPanel.setBackground(null);
        teaPanel.setOpaque(false);
        scrollPane.setOpaque(false);
        scrollPane.getViewport().setOpaque(false);
        setLocation();
        setAll();
        addAll();
        setAllListener();
    }
    public void setCart(int num) {
        setVisible(false);
        cartScrollPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        if(num == 0) {
            add(cartScrollPanel);
            layout.putConstraint(SpringLayout.EAST, cartScrollPanel,-10, SpringLayout.EAST, OrderPanel.this);
            layout.putConstraint(SpringLayout.WEST, cartScrollPanel,10, SpringLayout.EAST, scrollPane);
            layout.putConstraint(SpringLayout.SOUTH, cartScrollPanel,0, SpringLayout.NORTH, cartLabel);
            layout.putConstraint(SpringLayout.NORTH, cartScrollPanel,-500, SpringLayout.NORTH, cartLabel);
        }
        else remove(cartScrollPanel);
        cartScrollPanel.setOpaque(false);
        cartScrollPanel.getViewport().setOpaque(false);
        teaPanel.setVisible(false);
        teaPanel.setVisible(true);
        setVisible(true);
    }
    public void setLocation() {
        layout.putConstraint(SpringLayout.WEST, search,10, SpringLayout.WEST,this);
        layout.putConstraint(SpringLayout.NORTH, search,10, SpringLayout.NORTH,this);
        layout.putConstraint(SpringLayout.SOUTH, search,50, SpringLayout.NORTH,this);
        layout.putConstraint(SpringLayout.EAST, search,-250, SpringLayout.HORIZONTAL_CENTER,this);
        layout.putConstraint(SpringLayout.WEST, comboBox, 10, SpringLayout.WEST, this);
        layout.putConstraint(SpringLayout.NORTH, comboBox, 0, SpringLayout.SOUTH, search);
        layout.putConstraint(SpringLayout.SOUTH, comboBox, 0, SpringLayout.SOUTH, search);
        layout.putConstraint(SpringLayout.EAST, comboBox, 0, SpringLayout.EAST, search);
        layout.putConstraint(SpringLayout.NORTH, scrollPane, 100, SpringLayout.NORTH, this);
        layout.putConstraint(SpringLayout.SOUTH, scrollPane, -10, SpringLayout.SOUTH, this);
        layout.putConstraint(SpringLayout.WEST, scrollPane, -300, SpringLayout.HORIZONTAL_CENTER, this);
        layout.putConstraint(SpringLayout.EAST, scrollPane, -300, SpringLayout.EAST, this);
        layout.putConstraint(SpringLayout.NORTH, kindScrollPanel,50, SpringLayout.SOUTH, search);
        layout.putConstraint(SpringLayout.WEST, kindScrollPanel,3, SpringLayout.WEST,this);
        layout.putConstraint(SpringLayout.EAST, kindScrollPanel,-6, SpringLayout.WEST, scrollPane);
        layout.putConstraint(SpringLayout.SOUTH, kindScrollPanel,10, SpringLayout.SOUTH,this);
        layout.putConstraint(SpringLayout.SOUTH, cartLabel,-10, SpringLayout.SOUTH,this);
        layout.putConstraint(SpringLayout.WEST, cartLabel,-280, SpringLayout.EAST,this);
        layout.putConstraint(SpringLayout.EAST, cartLabel,-90, SpringLayout.EAST,this);
        layout.putConstraint(SpringLayout.NORTH, cartLabel,-80, SpringLayout.SOUTH,this);
        layout.putConstraint(SpringLayout.SOUTH, buyButton,0, SpringLayout.SOUTH, cartLabel);
        layout.putConstraint(SpringLayout.EAST, buyButton,0, SpringLayout.EAST, cartLabel);
        layout.putConstraint(SpringLayout.NORTH, buyButton,0, SpringLayout.NORTH, cartLabel);
        layout.putConstraint(SpringLayout.SOUTH, emptyButton,0, SpringLayout.SOUTH, buyButton);
        layout.putConstraint(SpringLayout.WEST, emptyButton,0, SpringLayout.EAST, buyButton);
        layout.putConstraint(SpringLayout.NORTH, emptyButton,0, SpringLayout.NORTH, buyButton);
        layout.putConstraint(SpringLayout.EAST, emptyButton,80, SpringLayout.EAST, buyButton);
        layout.putConstraint(SpringLayout.NORTH, numberLabel,-80, SpringLayout.SOUTH,this);
        layout.putConstraint(SpringLayout.WEST, numberLabel,65, SpringLayout.WEST, cartLabel);
    }
    public void setAllListener() {
        cartLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (b) {
                    setCart(0);
                    b = false;
                }
                else {
                    setCart(1);
                    b = true;
                }
            }
        });

        buyButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (!AddToCartBusiness.cart.orderItemList.isEmpty())
                    new DispositionSettleDialog(getOrder(), 2).setVisible(true);
            }
        });
        emptyButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                clean();
            }
        });
        search.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                if (!search.getText().isEmpty()) {
                    search.setFocusable(true);
                    scrollPane.setVisible(false);
                    kindScrollPanel.setVisible(false);
                }
                else {
                    if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
                        MilkTeaBusiness.setMilkTeas(1);
                        MilkTeaBusiness.setKinds();
                        layout.putConstraint(SpringLayout.NORTH, scrollPane, 100, SpringLayout.NORTH, OrderPanel.this);
                        layout.putConstraint(SpringLayout.WEST, scrollPane, -300, SpringLayout.HORIZONTAL_CENTER, OrderPanel.this);
                        kindScrollPanel.setVisible(true);
                        teaPanel.setVisible(false);
                        teaPanel.setVisible(true);
                        scrollPane.setVisible(true);
                        setVisible(false);
                        setVisible(true);
                        search.setText("");
                        search.setFocusable(true);
                        search.requestFocus();
                    }
                }
                if (e.getKeyCode() != KeyEvent.VK_ENTER) {
                    comboBox.update(search.getText());
                    comboBox.hidePopup();
                    comboBox.showPopup();
                }
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    teaPanel.setVisible(false);
                    teaPanel.removeAll();
                    layout.putConstraint(SpringLayout.NORTH, scrollPane, 10, SpringLayout.SOUTH, search);
                    layout.putConstraint(SpringLayout.WEST, scrollPane, 10, SpringLayout.WEST, OrderPanel.this);
                    boolean b = true;
                    int num = 0;
                    for (int i = 0; i < MilkTeaBusiness.lists.size(); i++) {
                        if (MilkTeaBusiness.teaItemPanels[i].milkTea.milkName.contains(search.getText())) {
                            teaPanel.add(MilkTeaBusiness.teaItemPanels[i]);
                            if (b) {
                                b = false;
                                teaLayout.putConstraint(SpringLayout.NORTH, MilkTeaBusiness.teaItemPanels[i], 5, SpringLayout.NORTH, teaPanel);
                                teaLayout.putConstraint(SpringLayout.SOUTH, MilkTeaBusiness.teaItemPanels[i], 150, SpringLayout.NORTH, teaPanel);
                            }
                            else {
                                teaLayout.putConstraint(SpringLayout.NORTH, MilkTeaBusiness.teaItemPanels[i], 1, SpringLayout.SOUTH, MilkTeaBusiness.teaItemPanels[num]);
                                teaLayout.putConstraint(SpringLayout.SOUTH, MilkTeaBusiness.teaItemPanels[i], 150, SpringLayout.SOUTH, MilkTeaBusiness.teaItemPanels[num]);
                            }
                            teaLayout.putConstraint(SpringLayout.WEST, MilkTeaBusiness.teaItemPanels[i], 1, SpringLayout.WEST, teaPanel);
                            teaLayout.putConstraint(SpringLayout.EAST, MilkTeaBusiness.teaItemPanels[i], -1, SpringLayout.EAST, teaPanel);
                            num = i;
                        }
                    }
                    setVisible(false);
                    setVisible(true);
                    search.setFocusable(true);
                    teaPanel.setVisible(true);
                    scrollPane.setVisible(true);
                    search.requestFocus();
                }
            }
        });
        comboBox.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (e.getModifiers() == 16)
                    search.setText((String) comboBox.getSelectedItem());
            }
        });
    }

    public void addAll() {
        add(buyButton);
        add(scrollPane);
        add(search);
        add(kindScrollPanel);
        add(numberLabel);
        add(cartLabel);
        add(emptyButton);
        add(comboBox);
    }

    public void setAll() {
        setLayout(layout);
        buyButton.setFont(new Font("", Font.BOLD, 20));
        emptyButton.setFont(new Font("", Font.BOLD, 20));
        jPanel.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
        kindPanel.setLayout(kindLayout);
        teaPanel.setLayout(teaLayout);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
        scrollPane.getVerticalScrollBar().setUnitIncrement(15);
        cartLabel.setFont(new Font("", Font.BOLD,35));
        cartLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        cartLabel.setForeground(Color.BLACK);
        cartLabel.setOpaque(true);
        numberLabel.setForeground(Color.RED);
        numberLabel.setFont(new Font("",Font.BOLD,20));
        kindScrollPanel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
        kindScrollPanel.setBorder(null);
        kindScrollPanel.getVerticalScrollBar().setUnitIncrement(15);
    }
    public Order getOrder() {
        Order order = new Order();
        order.orderItems = AddToCartBusiness.cart.orderItemList;
        order.total = AddToCartBusiness.cart.total;
        order.setDate();
        order.setTotalPrice();
        return order;
    }
    public static void clean() {
        for (int i = 0; i < MilkTeaBusiness.teaItemPanels.length; i++) {
            MilkTeaBusiness.teaItemPanels[i].numberLabel.setText("0");
        }
        AddToCartBusiness.cart.orderItemList.clear();
        AddToCartBusiness.map.clear();
        AddToCartBusiness.setOrderItems();
    }
}
